﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class SetIterator
    {

        public SetIterator()
        {
            MySet S = new MySet(new object[] { 1, 2, 3 });
            IEnumerator t = S.GetEnumerator();
            t.MoveNext();
            Console.WriteLine(t.Current);
            t.MoveNext();
            Console.WriteLine(t.Current);
            /*t.MoveNext();
            print( t.Current);
            t.MoveNext();
            print( t.Current);*/
            foreach (var item in S)
            {
                Console.WriteLine(t.Current);
            }


        
    }
}

    public class MySet : IEnumerable
    {
        internal object[] values;

        public MySet(object[] objs)
        {
            this.values = objs;
        }

        public IEnumerator GetEnumerator()
        {
            return new MySetIterator(this);
        }
    }

    public class MySetIterator : IEnumerator
    {
        MySet set;
        int position;
        internal MySetIterator(MySet set)
        {
            this.set = set;
            position = -1;
        }


        public object Current
        {
            get
            {
                if (position == -1 || position == set.values.Length)
                {
                    throw new InvalidOperationException("姜泽方烧坏了");
                }
                int index = position;
                return set.values[index];
            }
        }

        public bool MoveNext()
        {
            if (position != set.values.Length)
            {
                position++;
            }
            return position < set.values.Length;
        }


        public void Reset()
        {
            position = -1;
        }
    }
}
